package tech.aistar.corejava.day12.factory;

import tech.aistar.corejava.day12.Book;

import java.util.Comparator;

/**
 * @author success
 * @version 1.0
 * @description:本类用来演示:简单工厂模式
 * @date 2019/7/30 0030
 */
public class ComparatorFactory {

    public static final int NAME = 0;

    public static final int PRICE = 1;

    //根据传入的一个参数,来返回某个具体的实例
    //面向接口编程 - 方法的返回类型写成接口,方法的返回值可以是这个接口的具体的实现类.
    public static Comparator<Book> getInstance(int type){
        Comparator<Book> c = null;

        switch (type){
            case NAME:
                //匿名的内部类
                c = new Comparator<Book>() {
                    @Override
                    public int compare(Book o1, Book o2) {
                        return o1.getBookName().compareTo(o2.getBookName());
                    }
                };
                break;
            case PRICE:
                c = new Comparator<Book>() {
                    @Override
                    public int compare(Book o1, Book o2) {
                        if(o1.getPrice()>o2.getPrice())
                            return -1;
                        else if(o1.getPrice()<o2.getPrice())
                            return 1;
                        return 0;
                    }
                };
                break;
        }
        return c;
    }
}
